package com.muse.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @description 发送端——客户端
 * @author: muse
 **/
public class UdpClient {
    public final static String IP = "127.0.0.1";
    public final static int PORT = 8080;
    public static void main(String[] args) throws Throwable {
        DatagramSocket socket = new DatagramSocket(0);
        socket.setSoTimeout(10000); // 超时时间为10秒
        byte[] sendBuff = "hello".getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendBuff, sendBuff.length, InetAddress.getByName(IP), PORT);
        DatagramPacket recePacket = new DatagramPacket(new byte[10], 10);
        socket.send(sendPacket);
        socket.receive(recePacket);
        String result = new String(recePacket.getData(), 0, recePacket.getLength(), "ASCII");
        System.out.println("Receive = " + result);
    }
}
